package com.carkeeper.evaluate.service.impl;

import com.carkeeper.evaluate.dao.EvaluateDao;
import com.carkeeper.evaluate.pojo.Evaluate;
import com.carkeeper.evaluate.service.EvaluateService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zhangmingxin
 * @version 1.0
 * @date 2020/11/23 19:01
 */
@Service
public class EvaluateServiceImpl implements EvaluateService {

    @Autowired
    private EvaluateDao evaluateDao;

    /**
     * 列表查询
     *
     * @return
     */
    @Override
    public List<Evaluate> findAll() {
        return evaluateDao.selectAll();
    }


    /**
     * 条件查询
     *
     * @param searchMap
     * @return
     */
    @Override
    public Page<Evaluate> searchMap(Map<String, Object> searchMap, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        Example  example = this.createExample(searchMap);
        return (Page<Evaluate>) evaluateDao.selectByExample(example);
    }

    /**
     * 条件判断
     * @param searchMap
     * @return
     */
    public Example createExample(Map searchMap){
        //1.构建条件对象进行查询
        Example example = new Example(Evaluate.class);
        Example.Criteria criteria = example.createCriteria();
        //1.进行条件查询
        if (searchMap != null) {
            //2.判断订单编号
            if (searchMap.get("orderId") != null && !"".equals(searchMap.get("orderId"))) {
                criteria.andEqualTo("orderId", "%" + searchMap.get("orderId") + "%");
            }
            //3.判断客户名称
            if (searchMap.get("customorName") != null && !"".equals(searchMap.get("customorName"))) {
                criteria.andEqualTo("orderId", "%" + searchMap.get("customorName") + "%");
            }
            //4.服务类别更改
            if (searchMap.get("serviceType") != null && !"".equals(searchMap.get("serviceType"))) {
                criteria.andEqualTo("orderId", "%" + searchMap.get("serviceType") + "%");
            }
            //5.订单时间
            if (searchMap.get("startTime") != null && searchMap.get("endTime") != null) {
                //开始时间小于创建时间
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                String startTime = (String) searchMap.get("startTime");
                String endTime = (String) searchMap.get("endTime");
                Date startTime1 = null;
                Date endTime1 = null;
                try {
                    startTime1 = simpleDateFormat.parse(startTime);
                    endTime1 = simpleDateFormat.parse(endTime);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                criteria.andLessThanOrEqualTo("createTime", endTime1);
                //创建时间大于开始时间
                criteria.andGreaterThanOrEqualTo("createTime", startTime1);
            }
            //6.服务门店
            if (searchMap.get("storeSimpleName") != null && !"".equals(searchMap.get("storeSimpleName"))) {
                criteria.andEqualTo("orderId", "%" + searchMap.get("storeSimpleName") + "%");
            }
        }
        return example;
    }



    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public Evaluate findById(Integer id) {
      Evaluate evaluate = evaluateDao.selectByPrimaryKey(id);
     //   Evaluate evaluate = evaluateDao.findById(id);
        return evaluate;
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @Override
    public Integer deleteById(String id) {
        return evaluateDao.deleteByPrimaryKey(id);

    }
}
